package core.playerManager.swfManager
{
	import core.modelManager.ModelManager;
	import core.pluginManager.PluginManager;
	
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	import memory.MemoryRecovery;
	
	import view.player.SerialSwfPlayer;
	import view.player.SerialSwfPlayerEvent;
	
	public class SwfManager extends EventDispatcher
	{
		private static var instance:SwfManager;
		private var dataBase:Dictionary=new Dictionary(true);
		private var swfList:Dictionary=new Dictionary(true);
		public function SwfManager()
		{
		   if(instance==null)
			{
				instance=this;
			}else
			{
				throw new Error("不能实例化");
			}
		}
		public static function getInstance():SwfManager
		{
			if(instance==null) return new SwfManager();
			return instance;
		}
		public function init():void
		{
			initApi();
			initDatabase();
		}
		private function initApi():void
		{
//			ScriptManager.getInstance().addApi(ScriptName.REMOVE_SWF_BY_ID,this.removeSwfById);
//			ScriptManager.getInstance().addApi(ScriptName.SHOW_SWF_BY_ID,this.showSwfById);
		}
		private function initDatabase():void
		{
			
			var data:XML=ModelManager.getInstance().getXml("swf");
			var i:int=0;var j:int=0;
			var temp:Object;
			for(i=0;i<data.Swf.length();i++)
			{
				temp=new Object();
				var tempData:XML=data.Swf[i];
				var swfId:String=tempData.@id;
				var swfBgurl:String=tempData.@bgUrl;
				var swfFloor:int=tempData.@floor?(tempData.@floor):2;
				var swfX:Number=tempData.@x?tempData.@x:0;
				var swfY:Number=tempData.@y?tempData.@y:0;
				var swfSceneId:int=tempData.@sceneId;
				temp={id:swfId,bgUrl:swfBgurl,floor:swfFloor,x:swfX,y:swfY,sceneId:swfSceneId};
				var smallSwfs:Array=new Array();
				for(j=0;j<tempData.SmallSwf.length();j++)
				{
					var smallSwfData:XML=tempData.SmallSwf[j];
					smallSwfs.push({x:Number(smallSwfData.@x),y:Number(smallSwfData.@y),scaleX:Number(smallSwfData.@scaleX),scaleY:Number(smallSwfData.@scaleY),url:String(smallSwfData.@url)});
					temp.smallSwfs=smallSwfs;
				}
				var id:String=temp.id;
				this.dataBase[id]=temp;
			}
		}
		public function showSwfById(id:String):Boolean
		{
			if(this.dataBase[id]==null) return false;
			var data:Object=this.dataBase[id];
			var ssp:SerialSwfPlayer=new SerialSwfPlayer(data.bgUrl,data.smallSwfs);
			ssp.addEventListener(SerialSwfPlayerEvent.COMPLETE,function():void{
			    dispatchEvent(new SwfManagerEvent(SwfManagerEvent.COMPLETE));
			},false,0,true);
			ssp.addEventListener(SerialSwfPlayerEvent.READY,on_ready);
			swfList[id]=ssp;
			ssp.start();
			PluginManager.getInstance().getFloor(data.floor).addChild(ssp);
			ssp.x=data.x;
			ssp.y=data.y;
			return true;
		}
		private function on_ready(e:SerialSwfPlayerEvent):void
		{
			dispatchEvent(new SwfManagerEvent(SwfManagerEvent.READY));
		}
		public function removeSwfById(id:String):Boolean
		{
			var ssp:SerialSwfPlayer=swfList[id];
			if(ssp==null) return false;
			MemoryRecovery.getInstance().gcFun(ssp,SerialSwfPlayerEvent.READY,on_ready);
			if(ssp.parent!=null)
			{
				ssp.parent.removeChild(ssp);
			}
			ssp.dispose();
			ssp=null;
			swfList[id]=null;
			return true;
		}
	}
}